const BaseHandler = require('../base-handler');
const SystemServices = require('../../../constants').SystemServices;
const HTTP_VERBS = require('../../../constants').HTTP_VERBS;
const utils = require('../../../utils');
const moment = require('moment');

class DashboardDownloadHandler extends BaseHandler {
    get pattern() {
        return "/dashboard/download/:type";
    }

    doGET(request, response) {
        if (!request.session.user.authenticated) {
            response.json({
                success: false,
                error: '请登陆后再调用此API!'
            });
        } else {
            let params = JSON.parse(request.query.data),
                type = request.params.type;
            params.storeId = request.session.user.storeId;
            params.type = type;
            this.Controller.getOccupyData(params, (result) => {
                var arr = [];
                if (result.status) {
                    arr = result.data;
                }
                var obj = { date: '日期', count: '数量' },
                    nameList = {
                        traffic: 'dashboard-traffic.csv',
                        total: 'dashboard-total.csv',
                        gender: 'dashboard-gender.csv',
                        age: 'dashboard-age.csv',
                        sequential: 'dashboard-sequential.csv',
                        vipIncrease: 'dashboard-vipIncrease.csv',
                        amountAndCount: 'dashboard-amount-count.csv',
                        
                    }, name = '';
                switch (type) {
                    case 'age':
                        obj = { ageGroup: '年龄段', count: '数量' };
                        break;
                    case 'sequential':
                        arr = [];
                        obj = { date: '日期', current: '最近七天客流量', before: '环比七天客流量' };
                        result.data.current.forEach((item, index) => {
                            arr.push({ date: item.date, current: item.count, before: result.data.before[index].count });
                        })
                        break;
                    case 'vipIncrease':
                        obj = { date: '日期', vipCount: '新增会员数量', totalVipCount: '会员总数' };
                        break;
                    case 'total':
                        obj = { isvip: '客户类型', count: '数量' };
                        break;
                    case 'gender':
                        obj = { gender: '性别', count: '数量' };
                        break;
                    case 'amountAndCount':
                        obj = { date: '日期', amount: '销售额', count: '客流量' };
                        break;
                }
                name = nameList[type];
                arr.unshift(obj);

                utils.exportCSV(request, response, name, arr);
            });
        }
    }

    setup() {
        this.registerVerb(HTTP_VERBS.GET, this.doGET);
    }


}

module.exports = DashboardDownloadHandler;